<cfif Not IsDefined("session.id")><!---not logged in--->
	<cfset session.message = "Not logged in!">
	<cflocation url="/ecada/index.cfm"/><!---redirect to home where they will be able to log in--->
</cfif>



<cfif Not IsDefined("u") OR NOT IsDefined("g")><!--- make sure variabls are defined--->
	<cfset session.message = "Incorrect Parameters!">
	<cflocation url="ListGroups.cfm"/><!---if not redirect--->
</cfif>

<cfset UID = u>
<cfset groupID = g>

<!---get the owner ID of the group--->

<cfquery datasource="exp" name="a">

	SELECT ownerID
	FROM groups
	WHERE id = #groupID#

</cfquery>
<!---the current user can remove themselfs from a group
     the owner of a group can also remove users from the froup
     all other users cannot remove other users from other groups--->

<cfif session.id eq a.ownerID OR UID eq session.id>
	<cfif a.ownerID eq UID>
		<cfset session.message = "Cannot remove the owner from the group!">
		<cflocation url="ListGroups.cfm">
	<cfelse>
		<!---DELETE SINGLE USER FROM GROUP---->
		<cftransaction>
			<cfquery name="User" datasource="exp">
			
				SELECT u.username, g.name
				FROM user u, groupusers gus , groups g
				WHERE u.id = #UID#
				AND u.id = gus.UserID
				AND gus.GroupID = g.id
			
			</cfquery>
			<cfquery name="gID" datasource="exp">

				DELETE FROM groupusers
				WHERE UserID = #UID#
				AND GroupID = #groupID#

			</cfquery>	
		</cftransaction>
		<cfset session.message = "Sucessfully removed, #User.username#, from #User.name#!">
		<cflocation url="ListGroups.cfm">
	</cfif>
<cfelse>
	<cfquery name="User" datasource="exp">
	
		SELECT u.username, g.name
		FROM user u, groupusers gus , groups g
		WHERE u.id = #UID#
		AND u.id = gus.UserID
		AND gus.GroupID = g.id
	
	</cfquery>
	<cfset session.message ="You do not have permission to perform this action!">
	<cflocation url="ListGroups.cfm">
</cfif>